package com.xuecheng.media.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xuecheng.media.mapper.MediaFilesMapper;
import com.xuecheng.media.mapper.MediaProcessHistoryMapper;
import com.xuecheng.media.mapper.MediaProcessMapper;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.model.po.MediaProcess;
import com.xuecheng.media.model.po.MediaProcessHistory;
import com.xuecheng.media.service.MediaFileProcessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;

/**
 * @author ljx
 * date: 2024/06/15/15:20
 * description:MediaFileProcess接口实现
 */
@Service
@Slf4j
public class MediaFileProcessServiceImpl implements MediaFileProcessService {
    private final MediaProcessMapper mediaProcessMapper;
    private final MediaFilesMapper mediaFilesMapper;
    private final MediaProcessHistoryMapper mediaProcessHistoryMapper;

    public MediaFileProcessServiceImpl(MediaProcessMapper mediaProcessMapper, MediaFilesMapper mediaFilesMapper, MediaProcessHistoryMapper mediaProcessHistoryMapper) {
        this.mediaProcessMapper = mediaProcessMapper;
        this.mediaFilesMapper = mediaFilesMapper;
        this.mediaProcessHistoryMapper = mediaProcessHistoryMapper;
    }

    @Override
    public List<MediaProcess> getMediaProcessList(int shardIndex, int shardTotal, int count) {
        return mediaProcessMapper.selectListByShardIndex(shardTotal, shardIndex, count);
    }

    /**
     *  开启一个任务
     * @param id 任务id
     * @return true开启任务成功，false开启任务失败
     */
    @Override
    public boolean startTask(long id) {
        int result = mediaProcessMapper.startTask(id);
        return result > 0;
    }

    @Override
    public void saveProcessFinishStatus(Long taskId, String status, String fileId, String url, String errorMsg) {
        //要更新的任务
        MediaProcess mediaProcess = mediaProcessMapper.selectById(taskId);
        if (mediaProcess == null) {
            return;
        }
        //如果任务执行失败
        if (status.equals("3")) {
            mediaProcess.setStatus("3");
            mediaProcess.setFailCount(mediaProcess.getFailCount()+1);//失败次数+1
            mediaProcess.setErrormsg(errorMsg);
            mediaProcessMapper.updateById(mediaProcess);
            // 高效的更新方式
            /*LambdaUpdateWrapper<MediaProcess> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(MediaProcess::getId,mediaProcess.getId())
                    .set(MediaProcess::getStatus,"3")
                    .set(MediaProcess::getStatus,mediaProcess.getFailCount()+1)
                    .set(MediaProcess::getErrormsg,errorMsg);
            mediaProcessMapper.update(null,updateWrapper);*/
            return;
        }
        //如果任务执行成功
        if (status.equals("2")){
            //更新media_file表中url
            MediaFiles mediaFiles = mediaFilesMapper.selectById(mediaProcess.getFileId());
            mediaFiles.setUrl(url);
            mediaFilesMapper.updateById(mediaFiles);
            //更新media_process的状态
            mediaProcess.setStatus("2");
            mediaProcess.setFinishDate(LocalDateTime.now());
            mediaProcess.setUrl(url);
            mediaProcessMapper.updateById(mediaProcess);
            //将media_process插入到历史表
            MediaProcessHistory mediaProcessHistory = new MediaProcessHistory();
//            mediaProcessHistory.setFilePath(url);
            BeanUtils.copyProperties(mediaProcess,mediaProcessHistory);
            mediaProcessHistoryMapper.insert(mediaProcessHistory);
            //删除待处理任务表
            mediaProcessMapper.deleteById(taskId);
        }
    }
}
